package com.cn.intelligence.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.intelligence.mapper.TbItemDescMapper;
import com.cn.intelligence.mapper.TbItemMapper;
import com.cn.intelligence.mapper.TbItemParamItemMapper;
import com.cn.intelligence.pojo.EUDataGridResult;
import com.cn.intelligence.pojo.TaotaoResult;
import com.cn.intelligence.pojo.TbItem;
import com.cn.intelligence.pojo.TbItemDesc;
import com.cn.intelligence.pojo.TbItemDescExample;
import com.cn.intelligence.pojo.TbItemExample;
import com.cn.intelligence.pojo.TbItemParamItem;
import com.cn.intelligence.pojo.TbItemParamItemExample;
import com.cn.intelligence.service.ItemService;
import com.cn.intelligence.util.IDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Created by Administrator on 2017/4/17.
 */
@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper tbItemDescMapper;
	@Autowired
	private TbItemParamItemMapper tbItemParamItemMapper;

	@Override
	public TbItem getItemById(long itemId) {

		// TbItem item = itemMapper.selectByPrimaryKey(itemId);
		// 添加查询条件
		TbItemExample example = new TbItemExample();
		TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(itemId);
		example.setOrderByClause("stortOrder");
		List<TbItem> list = itemMapper.selectByExampleWithBLOBs(example);
		if (list != null && list.size() > 0) {
			TbItem item = list.get(0);
			return item;
		}
		return null;
	}

	/**
	 * 获取分页信息
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	@Override
	public EUDataGridResult getItemList(int page, int rows, String selectPoint, String selectName,
			String selectpinpai) {
		TbItemExample tbItemExample = new TbItemExample();
		TbItemExample.Criteria criteria = tbItemExample.createCriteria();
		if (selectPoint != null && !"".equals(selectPoint)) {
			try {
				selectPoint = URLDecoder.decode(selectPoint, "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			criteria.andIntroLike("%" + selectPoint + "%");
		}
		if (selectName != null && !"".equals(selectName)) {
			try {
				selectName = URLDecoder.decode(selectName, "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			criteria.andNameLike("%" + selectName + "%");
		}
		if (selectpinpai != null && !"".equals(selectpinpai)) {
			try {
				selectpinpai = URLDecoder.decode(selectpinpai, "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			criteria.andPinpaiLike("%" + selectpinpai + "%");
		}
		tbItemExample.setOrderByClause("created DESC, updated DESC");
		// 分页查询
		PageHelper.startPage(page, rows);
		List<TbItem> tbItemList = itemMapper.selectByExampleWithBLOBs(tbItemExample);
		// 获取总数
		PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(tbItemList);
		// 实现返回值设定
		EUDataGridResult euDataGridResult = new EUDataGridResult();
		euDataGridResult.setRows(tbItemList);
		euDataGridResult.setTotal(pageInfo.getTotal());
		return euDataGridResult;

	}

	/**
	 * 保存一个商品信息
	 * 
	 * @param item
	 * @return
	 */
	@Override
	public TaotaoResult saveItem(TbItem item, String desc, String paramData) throws Exception {
		// 生产一个产品Id
		long itemId = IDUtils.genItemId();
		item.setId(itemId);
		// 生产商品状态
		item.setStatus((byte) 1);
		item.setIsHot(item.getIsHot() == null || "".equals(item.getIsHot()) ? 1 : item.getIsHot());
		item.setIsShow(item.getIsShow() == null || "".equals(item.getIsHot()) ? 1 : item.getIsShow());
		item.setShiyong(100);
		// 添加商品常见修改时间
		item.setCreated(new Date());
		item.setUpdated(new Date());
		// 插入商品
		itemMapper.insert(item);
		// 插入商品详情
		TbItemDesc tbItemDesc = new TbItemDesc();
		tbItemDesc.setItemId(itemId);
		tbItemDesc.setItemDesc(desc);
		tbItemDesc.setCreated(new Date());
		tbItemDesc.setUpdated(new Date());
		int resut = tbItemDescMapper.insert(tbItemDesc);
		if (resut <= 0) {
			throw new Exception();
		}
		TbItemParamItem tbItemParamItem = new TbItemParamItem();
		tbItemParamItem.setItemId(itemId);
		tbItemParamItem.setParamData(paramData);
		tbItemParamItem.setCreated(new Date());
		tbItemParamItem.setUpdated(new Date());
		resut = tbItemParamItemMapper.insert(tbItemParamItem);
		if (resut <= 0) {
			throw new Exception();
		}
		return TaotaoResult.ok();
	}

	/**
	 * 删除该商品
	 * 
	 * @throws Exception
	 */
	@Override
	public TaotaoResult deleteItem(Long itemId[]) throws Exception {

		int value = 0;
		for (Long long1 : itemId) {
			TbItemExample tbItemexample = new TbItemExample();
			TbItemExample.Criteria criteria = tbItemexample.createCriteria();
			criteria.andIdEqualTo(long1);
			value += itemMapper.deleteByExample(tbItemexample);
		}
		if (value == itemId.length) {
			return TaotaoResult.ok();
		} else {
			throw new Exception();
		}
	}

	@Override
	public TaotaoResult editItem(TbItem item, String desc, String paramData) throws Exception {

		// 查询给商品是否存在
		TbItemExample example = new TbItemExample();
		TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(item.getId());
		List<TbItem> list = itemMapper.selectByExample(example);
		if (list == null || list.size() == 0) {
			return TaotaoResult.fail();
		}

		// 生产一个产品Id
		item.setId(item.getId());
		// 生产商品状态
		item.setStatus((byte) 1);
		// 添加商品常见修改时间
		item.setUpdated(new Date());
		// 修改商品
		itemMapper.updateByExampleSelective(item, example);
		// 插入商品详情
		TbItemDescExample descExample = new TbItemDescExample();
		TbItemDescExample.Criteria desccriteria = descExample.createCriteria();
		desccriteria.andItemIdEqualTo(item.getId());
		// 插入之前验证是否存在
		List<TbItemDesc> desclist = tbItemDescMapper.selectByExample(descExample);
		TbItemDesc tbItemDesc = new TbItemDesc();
		tbItemDesc.setItemId(item.getId());
		tbItemDesc.setItemDesc(desc);
		tbItemDesc.setCreated(new Date());
		tbItemDesc.setUpdated(new Date());
		// 如果不存在就插入
		if (desclist == null || desclist.size() == 0) {
			int resut = tbItemDescMapper.insert(tbItemDesc);
			if (resut <= 0) {
				throw new Exception();
			}
		} else {
			tbItemDesc.setCreated(desclist.get(0).getCreated());
			// 存在就修改
			int resut = tbItemDescMapper.updateByExampleSelective(tbItemDesc, descExample);
			if (resut <= 0) {
				throw new Exception();
			}
		}

		// 插入规格参数
		TbItemParamItemExample paramExample = new TbItemParamItemExample();
		TbItemParamItemExample.Criteria paramCriteria = paramExample.createCriteria();
		paramCriteria.andItemIdEqualTo(item.getId());
		List<TbItemParamItem> paramlist = tbItemParamItemMapper.selectByExample(paramExample);

		TbItemParamItem tbItemParamItem = new TbItemParamItem();
		tbItemParamItem.setItemId(item.getId());
		tbItemParamItem.setParamData(paramData);
		tbItemParamItem.setCreated(new Date());
		tbItemParamItem.setUpdated(new Date());
		// 如果不存在就插入
		if (paramlist == null || paramlist.size() == 0) {
			int resut = tbItemParamItemMapper.insert(tbItemParamItem);
			if (resut <= 0) {
				throw new Exception();
			}
		} else {
			tbItemParamItem.setCreated(paramlist.get(0).getCreated());
			// 存在就修改
			int resut = tbItemParamItemMapper.updateByExampleSelective(tbItemParamItem, paramExample);
			if (resut <= 0) {
				throw new Exception();
			}
		}
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult instockItem(Long[] ids) throws Exception {

		int value = 0;
		TbItemExample tbItemexample = new TbItemExample();
		TbItemExample.Criteria criteria = tbItemexample.createCriteria();
		criteria.andIdIn(Arrays.asList(ids));
		TbItem item = new TbItem();
		item.setStatus((byte) 2);
		value += itemMapper.updateByExampleSelective(item, tbItemexample);
		if (value == ids.length) {
			return TaotaoResult.ok();
		} else {
			throw new Exception();
		}
	}

	@Override
	public TaotaoResult reshelfItem(Long[] ids) throws Exception {

		int value = 0;
		TbItemExample tbItemexample = new TbItemExample();
		TbItemExample.Criteria criteria = tbItemexample.createCriteria();
		criteria.andIdIn(Arrays.asList(ids));
		TbItem item = new TbItem();
		item.setStatus((byte) 1);
		value += itemMapper.updateByExampleSelective(item, tbItemexample);
		if (value == ids.length) {
			return TaotaoResult.ok();
		} else {
			throw new Exception();
		}
	}

	/**
	 * 将商品推荐到首页
	 * 
	 * @throws Exception
	 */
	@Override
	public TaotaoResult pushhomeItem(Long[] ids, String stortOrder) throws Exception {
		int value = 0;
		if (ids.length == 1) {
			TbItemExample tbItemexample = new TbItemExample();
			TbItemExample.Criteria criteria = tbItemexample.createCriteria();
			criteria.andIdEqualTo(ids[0]);
			TbItem item = new TbItem();
			item.setStortorder(new Long(stortOrder));
			item.setUpdated(new Date());
			value += itemMapper.updateByExampleSelective(item, tbItemexample);
		} else {
			long index = 1;
			for (Long long1 : ids) {
				TbItemExample tbItemexample = new TbItemExample();
				TbItemExample.Criteria criteria = tbItemexample.createCriteria();
				criteria.andIdEqualTo(long1);
				TbItem item = new TbItem();
				item.setStortorder(index++);
				item.setUpdated(new Date());
				value += itemMapper.updateByExampleSelective(item, tbItemexample);
			}
		}
		if (value == ids.length) {
			return TaotaoResult.ok();
		} else {
			throw new Exception();
		}
	}

	/**
	 * 取消推荐到首页
	 * 
	 * @throws Exception
	 */
	@Override
	public TaotaoResult popHomeItem(Long[] ids, String stortOrder) throws Exception {
		int value = 0;
		for (Long long1 : ids) {
			TbItemExample tbItemexample = new TbItemExample();
			TbItemExample.Criteria criteria = tbItemexample.createCriteria();
			criteria.andIdEqualTo(long1);
			TbItem item = new TbItem();
			item.setStortorder(new Long(0));
			value += itemMapper.updateByExampleSelective(item, tbItemexample);
		}
		if (value == ids.length) {
			return TaotaoResult.ok();
		} else {
			throw new Exception();
		}
	}

	@Override
	public TbItem getItemPhotoXById(Long itemId) {
		TbItemExample tbItemexample = new TbItemExample();
		TbItemExample.Criteria criteria = tbItemexample.createCriteria();
		criteria.andIdEqualTo(itemId);
		List<TbItem> list = itemMapper.selectByExampleWithBLOBs(tbItemexample);
		if (list != null && list.size() > 0) {
			TbItem item = list.get(0);
			return item;
		} else {
			return null;
		}

	}

}
